package com.zzw.spring.aop;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.zzw.basic.io.Person;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor{

	private Enhancer enhancer = new Enhancer();

	public Object getProxy(Class clazz){
		enhancer.setSuperclass(clazz);
		enhancer.setCallback(this);
		return enhancer.create();
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("start");
		Object result = proxy.invokeSuper(obj, args);
		System.out.println(obj.getClass().getName());
		Class<?>[] interfaces = obj.getClass().getInterfaces();
		System.out.println(obj.getClass().getSuperclass().getName());
		System.out.println(getClass());
		System.out.println("end");
		return result;
	}

	public static void main(String[] args) {
		Class<Person> c = Person.class;
		Field name = null;
		try {
			Person person = c.newInstance();
			name = c.getDeclaredField("name");
			name.setAccessible(true);
			name.set(person,"123");
			System.out.println(person.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
